package cn.wei.sdd.core.service.impl;

import cn.wei.sdd.core.enums.TransStatusEnum;
import cn.wei.sdd.core.pojo.entity.TransFlow;
import cn.wei.sdd.core.mapper.TransFlowMapper;
import cn.wei.sdd.core.pojo.query.TransFlowQuery;
import cn.wei.sdd.core.pojo.vo.AdminTransFlowListVO;
import cn.wei.sdd.core.pojo.vo.SelectTimeVO;
import cn.wei.sdd.core.pojo.vo.UserTransFlowVO;
import cn.wei.sdd.core.service.TransFlowService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author Wei
 * @since 2021-09-22
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {

    @Override
    public List<UserTransFlowVO> getMyTransFlow(String userType, Long userId, SelectTimeVO selectTimeVO) {

        QueryWrapper<TransFlow> transFlowQueryWrapper = new QueryWrapper<>();

        transFlowQueryWrapper.eq("user_id",userId).eq("user_type",userType);
        String startTime = selectTimeVO.getStartTime();
        String endTime = selectTimeVO.getEndTime();

        if(startTime!=null&&endTime!=null){
            transFlowQueryWrapper.between("create_time",startTime,endTime);
        }
        transFlowQueryWrapper.orderByDesc("create_time");
        List<TransFlow> transFlowList = baseMapper.selectList(transFlowQueryWrapper);
        List<UserTransFlowVO> transFlowVOList = new ArrayList<>();
        for (TransFlow transFlow : transFlowList) {
            UserTransFlowVO userTransFlowVO = new UserTransFlowVO();
            userTransFlowVO.setTransNo(transFlow.getTransNo());
            userTransFlowVO.setTransType(transFlow.getTransType()==TransStatusEnum.DRAW.getStatus()?"-":"+");
            userTransFlowVO.setTransAmount(transFlow.getTransAmount());
            userTransFlowVO.setMemo(transFlow.getMemo());
            userTransFlowVO.setTransTypeName(transFlow.getTransTypeName());
            userTransFlowVO.setCreateTime(transFlow.getCreateTime());
            transFlowVOList.add(userTransFlowVO);
        }
        return transFlowVOList;
    }

    @Override
    public IPage<AdminTransFlowListVO> listPage(Page<AdminTransFlowListVO> pageParam, TransFlowQuery transFlowQuery) {

        if (transFlowQuery==null){
            return baseMapper.mySelectPage(pageParam,null);
        }
        else{
            return baseMapper.mySelectPage(pageParam,transFlowQuery);
        }

    }
}
